
# V figure 60 days: Intensity - share of death in X days after statr admission: ------------------------------


do_figure60_days_events <- function(top_All_prof_10=top_All_prof_10 ) {
  cnr_events_res <-  get_load_new(file_name = "revision_events_intensity.RData")
  events<- cnr_events_res$preds_test %>% 
    filter(EVE_category %in% c("elective","urgent") ) %>% 
             data.table() 

  events_tree <-  get_revision_cancer_events_full_data(con) %>%
    add_grouped_cancer_type() %>% 
    drop_dead_on_index_date() %>%
    filter(S_index_date_XX < DMG_date_of_death_XX | is.na(DMG_date_of_death_XX)) %>% 
    filter(EVE_intensity %in% c("High","Low")) %>% 
    collect()  %>% data.table()
  
  events_adm<-  merge(x = events , 
                      y = events_tree , 
                      by =  c("id_var","EVE_event_number"), 
                      all.x = T,
                      all.y = F)

  events_adm[,date_start_death := as.numeric(difftime(
    DMG_date_of_death_XX, S_index_date_XX, units = "days"))]
  
  events_adm[, `:=`(died_7_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=7) ,
                                      died_14_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=14) ,
                                      died_30_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=30) ,
                                      died_60_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=60) ) 
                               ]
  
  events_adm[,Intensity := ifelse(EVE_profession%in%intensity_wards,
                                                    "Low","High")]
  
  events_adm_Intensity <- events_adm[,
                                                               .("7 days"= mean(died_7_days),
                                                                 "14 days"= mean(died_14_days),
                                                                 "30 days"= mean(died_30_days),
                                                                 "60 days"= mean(died_60_days),
                                                                 N=as.double(.N)),
                                                               
                                                               by= .(Intensity , 
                                                                     bins = plyr::round_any(preds_after_bayes, 0.10, floor)) ][
                                                                       order(bins)]
  
  
  combine_death_adm_bins_Intensity <- rbind(
    events_adm_Intensity[,type:="At The Event"]
  )
  
  combine_death_adm_bins_long_Intensity <- 
    melt(combine_death_adm_bins_Intensity,
         id.vars= c("bins","type","Intensity") ,
         measure.vars = names(events_adm_Intensity)[3:6])
  
  combine_death_adm_bins_long_Intensity[,variable:=factor(variable, levels = c("60 days", "30 days", "14 days" ,"7 days"))]
  
  write.csv(combine_death_adm_bins_long_Intensity[variable=="60 days" &
                                              type == "At The Event",],
      file="Death_within_60_days_after_adm_Intensity_Events_Prediction.csv")
  
  
  pdf(file="Death_within_60_days_after_adm_Intensity_Events_Pred.pdf", width = 10, height = 7.5)
  print(ggplot()+
          geom_density(data = events_adm,
                       aes(x=preds_after_bayes, y=..scaled.., fill=Intensity),
                       size = 0.7,
                       color=NA,
                       alpha = 0.2) +
          geom_line(data =combine_death_adm_bins_long_Intensity[variable=="60 days" &
                                                                  type == "At The Event",],
                    aes(x=(bins+0.05) ,y= value, linetype = Intensity))+
          scale_x_continuous(breaks = seq(0, 1, 0.10), limits = c(0,1)) +
          scale_y_continuous(breaks = seq(0, 1, 0.10), limits = c(0,1)) +
          scale_fill_manual( values = c("grey5","grey50"))+
          labs(x = "Current Prognosis (One-Year Mortality Risk)",
               y = "Fraction of Admissions Ending in Death Within 60 Days",
               linetype="Intensity",
               fill ="Intensity")+
          ylim(0,1) +
          theme(legend.position = "none",
                axis.title.x = element_text(size = 16),
                axis.title.y = element_text(size = 16)) +
          theme(aspect.ratio = 1)   +
          annotate(geom="text", x=0.7, y=0.07, label="Low Intensity", size = 5)+
          annotate(geom="text", x=0.7, y=0.02, label="High Intensity", size = 5)+
          annotate(geom="text", x=0.59, y=0.32, label="Low Intensity", size = 5)+
          annotate(geom="text", x=0.84, y=0.27, label="High Intensity", size =5 )
  )
  dev.off()
  
}
